// pages/flea/mySend/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js");

import Toast from '../../../../component/vant/toast/toast';
import Dialog from '../../../../component/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordsList:[],
    pageIndex:1,
    pageSize:10,
    loading: false,
    netWork: true,
    isLoading: true,
    netWork:false,
    hasData:false,
    state:-1,
    currentTab:0,
    states:[-1, 0, 1, 2],
    userId:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.userId = app.getStorageSync("user_id");
    this.setData({
      userId : this.data.userId
    })
  },

  handleChange({ detail }) {
    this.data.currentTab = detail.index;
    this.data.state = this.data.states[detail.index];
    // if(this.data.isNewReq == 0){
    // }
    this.setData({
      currentTab: detail.index
    })
    this.initRequest(detail.index);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initRequest()
  },
  initRequest(){
    var that = this;
    that.data.pageIndex = 1;
    that.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordsList:[]
    });
    that.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/repair/record/list', {
      state: that.data.state,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      that.data.isNewReq = 0;
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex += 1;
          console.log("报修上报列表", res)
          var records = res.data.data;
          if(records.length > 0){
            for (var index in records) {
              that.data.recordsList.splice(that.data.recordsList.length, 0, records[index]);
            }
            that.setData({
              hasData: true,
              recordsList: that.data.recordsList
            })
          }
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  goAddRepair(){
    wx.navigateTo({
      url: '../add/index',
    })
  },
  goView(e){
    // wx.navigateTo({
    //   url: '../view/index',
    // })
    var record_id = e.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: '../view/index?id=' + record_id,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.initRequest(this.data.currentTab)
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for (var index in e) {
      c.push(e[index])
    }
    common_Js.preImg(a, c)
  },
  goDelInfo(e){
    var id = e.currentTarget.dataset.id
    var message = "是否要删除该信息？";
    var that = this
    Dialog.confirm({
      title: '删除发布',
      message
    }).then(() => {
      that.onHttpDeleteOp(id)
    })
    .catch(() => {
      Dialog.close();
    });
  },  
  onHttpDeleteOp: function (id){
    var that = this;
    fetchApi.httpPost('/repair/record/delete', {
      record_id:id,
    }, function (res) {
      console.log("删除返回数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
      } else {
        Toast.fail(res.data.msg);
      }
      that.initRequest()
    },function(){})
  },
  refreshRecord(){
    this.data.pageIndex = 1;
    this.setData({
      recordsList:[],
      hasData:false,
    })
    that.initRequest(0)
  },
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      isLoading:true,
      recordsList:[],
    })
    this.onHttpInit();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.onHttpInit();
  },
})